package com.crm.dao;

import com.crm.model.business.Paper;
import com.crm.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;

public class PaperDao {

    /**
     * 查询出所有的试卷
     * @return
     */
    public List<Paper> list() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Query query = session.createQuery("from Paper");
        @SuppressWarnings("unchecked")
        List<Paper> paperList = (List<Paper>) query.list();

        session.getTransaction().commit();
        return paperList;
    }

    /**
     * 根据id获取试卷
     */
    public Paper getPaperById(String id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Paper paper = (Paper) session.get(Paper.class, Integer.parseInt(id));

        session.getTransaction().commit();
        return paper;
    }

    /**
     * 删除试卷
     */
    public void delete(Paper paper) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        session.delete(paper);

        session.getTransaction().commit();
    }

    /**
     * 保存试卷
     */
    public void save(Paper paper) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        session.merge(paper);

        session.getTransaction().commit();
    }
}
